//
//  QWDashLineView.m
//  Pods
//
//  Created by ZhangYanhua on 16/2/23.
//
//

#import "QWDashLineView.h"

@implementation QWDashLineView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _lineColor = [UIColor colorWithRed:229.0f / 255.0f
                                 green:229.0f / 255.0f
                                  blue:229.0f / 255.0f
                                 alpha:1.0f];
    
    _lineWidth = 1.0f / [UIScreen mainScreen].scale;
    
    _lineDashPattern = @[@(4), @(2)];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGFloat thickness = _lineWidth;
    
    CGContextRef cx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(cx, thickness);
    CGContextSetStrokeColorWithColor(cx, _lineColor.CGColor);
    
    CGFloat *ra = malloc(sizeof(CGFloat) * _lineDashPattern.count);
    for (int i = 0; i < _lineDashPattern.count; i++) {
        ra[i] = [_lineDashPattern[i] floatValue];
    }
    CGContextSetLineDash(cx, 0.0, ra, _lineDashPattern.count); // nb "2" == ra count
    free(ra);
    
    CGContextMoveToPoint(cx, 0, thickness*0.5);
    CGContextAddLineToPoint(cx, self.bounds.size.width, thickness*0.5);
    CGContextStrokePath(cx);
}

@end
